<template>
  <div class="login-container">
    <div class="login-header">
      <h1>微风医药</h1>
    </div>
    <el-form :model="formInfo" label-width="0" class="login-form">
      <el-form-item class="form-item-center">
        <el-input v-model="formInfo.username" placeholder="请输入用户名">
          <template #prefix>
            <i class="el-icon-user"></i>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item class="form-item-center">
        <el-input v-model="formInfo.password" type="password" show-password placeholder="请输入密码">
          <template #prefix>
            <i class="el-icon-lock"></i>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item class="form-item-center">
        <el-button type="primary" @click="dologin" class="login-button">登录</el-button>
      </el-form-item>
      <el-form-item class="form-item-center">
        <el-button @click="register" class="register-button">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { login } from '@/api'
import { ElMessage } from 'element-plus'
import axios from 'axios'

export default {
  name: 'loginView',
  data() {
    return {
      formInfo: {
        username: '',
        password: ''
      },
      sessionToken: ''
    }
  },
  created() {
    this.checkSessionToken()
  },
  methods: {
    dologin() {
      let formData = new FormData()
      formData.append('username', this.formInfo.username)
      formData.append('password', this.formInfo.password)

      login('http://localhost:9099/user/login', formData).then(res => {
        if (res.data.code === 200) {
          localStorage.setItem('sessionToken', res.data.data.sessiontoken)
          localStorage.setItem('userId', res.data.data.id)
          localStorage.setItem('username', res.data.data.username)
          ElMessage({
            message: res.data.msg,
            type: 'success',
            duration: 2000
          })
          this.$router.push('/HomePage/HomeView')
        } else {
          ElMessage({
            message: res.data.msg,
            type: 'error',
            duration: 2000
          })
        }
      })
    },
    checkSessionToken() {
      const userid = localStorage.getItem('userId')
      if (userid !== null) {
        axios.get(`http://localhost:9099/user/verify?userid=${userid}`)
            .then(res => {
              if (res.data.code === 200) {
                localStorage.setItem('userId', res.data.data.id)
                localStorage.setItem('username', res.data.data.username)
                ElMessage({
                  message: res.data.msg,
                  type: 'success',
                  duration: 2000
                })
                this.$router.push('/HomePage/HomeView')
              } else {
                localStorage.removeItem('sessionToken')
                localStorage.removeItem('userId')
              }
            })
            .catch(error => {
              console.error('验证请求失败:', error)
              localStorage.removeItem('sessionToken')
              localStorage.removeItem('userId')
            })
      }
    },
    register() {
      this.$router.push('/registerView')
    }
  }
}
</script>
<style scoped>
.login-container {
  height: 100vh;
  background-image: url('@/static/img/qq .png');
  background-size: cover;
  background-position: center;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

.login-header h1 {
  color: #fff;
  font-size: 36px;
  font-weight: bold;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
  text-align: center;
  margin-bottom: 40px;
}

.login-form {
  max-width: 400px; /* 调整宽度 */
  width: 100%;
  background: #fff;
  padding: 30px; /* 增加内边距 */
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  border-radius: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.el-form-item {
  width: 100%;
  margin-bottom: 20px;
}

.el-form-item__inner {
  display: flex;
  justify-content: center;
}

.el-button {
  background-color: #3a7ff6;
  border-color: #3a7ff6;
  color: #fff;
  width: 100%;
}

.el-button:hover {
  background-color: #337eff;
  border-color: #337eff;
}

.el-input__icon {
  color: #666;
}
</style>